<?php
/**
 * city.php
 *
 * @copyright 2018 opencart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author StifferChen <chenlin@opencart.cn>
 * @created 2018-05-14 00:14
 * @modified 2018-05-14 00:14
 */

class ModelLocalisationCity extends Model
{
    public function getCity($city_id)
    {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "city WHERE city_id = '" . (int)$city_id . "'");

        return $query->row;
    }

    public function getCityByName($name, $zoneId = null)
    {
        if ($zoneId) {
            $sql = "SELECT * FROM " . DB_PREFIX . "city WHERE name = '" . $name . "' AND zone_id=" . (int)$zoneId;
        } else {
            $sql = "SELECT * FROM " . DB_PREFIX . "city WHERE name = '" . $name . "'";
        }
        $query = $this->db->query($sql);
        return $query->row;
    }

    public function getCountyByName($name, $cityId)
    {
        if ($cityId) {
            $sql = "SELECT * FROM " . DB_PREFIX . "city WHERE name = '" . $name . "' AND up_id=" . (int)$cityId;
        } else {
            return null;
        }
        $query = $this->db->query($sql);
        return $query->row;
    }

    public function getCities()
    {
        $city_data = $this->cache->get('city.status');

        if (!$city_data) {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "city WHERE status = '1' ORDER BY name ASC");

            $city_data = $query->rows;

            $this->cache->set('city.status', $city_data);
        }

        return $city_data;
    }

    public function getCitiesByZoneId($zoneId, $topLevel = true)
    {
        $cacheKey = 'city.' . (int)$zoneId . '_' . (int)$topLevel;
        $city_data = $this->cache->get($cacheKey);

        if (!$city_data) {
            if ($topLevel) {
                $sql = "SELECT * FROM " . DB_PREFIX . "city WHERE zone_id = '" . (int)$zoneId . "' AND status = '1' AND up_id=0 ORDER BY name";
            } else {
                $sql = "SELECT * FROM " . DB_PREFIX . "city WHERE up_id = '" . (int)$zoneId . "' AND status = '1' ORDER BY name";
            }
            $query = $this->db->query($sql);

            $city_data = $query->rows;
            $this->cache->set($cacheKey, $city_data);
        }

        return $city_data;
    }

    public function generateAddressCity($data)
    {
        $addressCity = '';
        if (isset($data['city_id']) && (int)$data['city_id'] > 0) {
            $city = $this->getCity($data['city_id']);
            $addressCity = $city['name'];
        }
        if (isset($data['county_id']) && (int)$data['county_id'] > 0) {
            $county = $this->getCity($data['county_id']);
            $addressCity = $addressCity . ' ' . $county['name'];
        }
        return $addressCity;
    }

    public function getCityIdFromAddress($data)
    {
        if (!isset($data['city'])) {
            return 0;
        }
        $cityDistrict = explode(' ', $data['city']);
        $city = $this->getCityByName($cityDistrict[0], $data['zone_id']);
        if (empty($city)) {
            return null;
        }
        return $city['city_id'];
    }

    public function getCountyIdFromAddress($data)
    {
        if (!isset($data['city'])) {
            return 0;
        }
        $cityDistrict = explode(' ', $data['city']);
        if (!isset($cityDistrict[1])) {
            return null;
        }
        $cityId = $this->getCityIdFromAddress($data);
        if (!$cityId) {
            return null;
        }
        $city = $this->getCountyByName($cityDistrict[1], $cityId);
        if (empty($city)) {
            return null;
        }
        return $city['city_id'];
    }
}